﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using Dtos;
using DAL;
using IDAL;
using BLL;
using Model;

namespace BLL
{
    public class StudentBll : IStudentBll
    {
        private IStudentDal _IS;
        public StudentBll(IStudentDal IS)
        {
            _IS = IS;
        }
        public async Task<int> AddAsync(string name, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, Guid gradesId, string pwd, string photo)
        {
            return await _IS.AddAsync(new Model.Students
            {
                name = name,
                birthday = birthday,
                gender = gender,
                tel = tel,
                email = email,
                qqNumber = qqNumber,
                address = address,
                gradesId = gradesId,
                qwd = pwd,
                photo = photo

            });
        }
      

        public async Task<int> DeleteAsync(Guid id)
        {
            var data = await _IS.GetIdAll(id);
            return data != null ? await _IS.DeleteAsync(data) : 0;
        }

        public async Task<int> EditAsync(Guid id, string name, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, Guid gradesId, string pwd, string photo)
        {
            var data = await _IS.GetIdAll(id);
            if (data == null)
                return 0;
            data.name = name;
            data.birthday = birthday;
            data.gender = gender;
            data.tel = tel;
            data.email = email;
            data.qqNumber = qqNumber;
            data.address = address;
            data.gradesId = gradesId;
            data.qwd = pwd;
            data.photo = photo;
            data.UpdateTime = DateTime.Now;
            return await _IS.UpdateAsync(data);
        }

        public async Task<List<StudentDtos>> getAllStudent()
        {
            var data = await _IS.GetAll();
            return data.Select(d => new StudentDtos
            {
                id = d.Id,
                name = d.name,
                birthday = d.birthday,
                gender = d.gender,
                tel = d.tel,
                email = d.email,
                qqNumber = d.qqNumber,
                address = d.address,
                gradesId = d.gradesId,
                pwd = d.qwd,
                photo = d.photo,
                updateTime = DateTime.Now
            }).OrderByDescending(d => d.updateTime).ToList();
        }

        public async Task<List<StudentDtos>> GetStudentByNameAsync(string title)
        {
            var data = await _IS.GetDataByChoose(a => a.name.Contains(title));
            return data == null ? null : data.Select(m => new StudentDtos
            {
                id = m.Id,
                name = m.name,
                gender = m.gender,
                birthday = m.birthday,
                tel = m.tel,
                email = m.email,
                qqNumber = m.qqNumber,
                address = m.address,
                gradesId = m.gradesId,
                pwd = m.qwd,
                photo = m.photo,
                updateTime = m.UpdateTime,
            }).OrderByDescending(g => g.updateTime).ToList();
        }

        public async Task<StudentDtos> GetStudentIdAsync(Guid id)
        {
            var data = await _IS.GetIdAll(id);
            return data != null ? new StudentDtos
            {
                id = data.Id,
                name = data.name,
                birthday = data.birthday,
                gender = data.gender,
                tel = data.tel,
                email = data.email,
                pwd = data.qwd,
                photo = data.photo,
                address = data.address,
                qqNumber = data.qqNumber,
                gradesId = data.gradesId,
                createTime = data.CreateTime,
                updateTime = data.UpdateTime,
                deleteId = data.DeleteId
            } : null;
        }

        public async Task<bool> IsExistsAsync(string title)
        {
            var data = await _IS.GetDataByChoose(m => m.name.Equals(title));
            return data.Count() > 0;
        }
        public async Task<StudentDtos> SignInAsync(string ac, string pwd)
        {
            var data = await _IS.GetDataByChoose(
                s => (ac.Equals(s.tel) || ac.Equals(s.email) && pwd.Equals(s.qwd) && s.DeleteId == 1)
            );

            return data.Count > 0 ? new StudentDtos
            {
                id = data[0].Id,
                name = data[0].name,
                birthday = data[0].birthday,
                gender = data[0].gender,
                tel = data[0].tel,
                email = data[0].email,
                pwd = data[0].qwd,
                photo = data[0].photo,
                address = data[0].address,
                qqNumber = data[0].qqNumber,
                gradesId = data[0].gradesId,
                createTime = data[0].CreateTime,
                updateTime = data[0].UpdateTime,
                deleteId = data[0].DeleteId
            } : null;

        }


    }
}
